﻿using ERP.ERPModels.Production.MasterData;
using ERP.ERPModels.WarehouseManagement;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using ERP.MasterData;
using ERP.MasterData.Dtos;
using ERP.ProductionManage.Dtos;
using ERP.WarehouseManagement;
using ERP.WarehouseManagement.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace ERP.Web.Controllers
{
    public class OutsourcingController : Controller
    {
        private readonly IOutsourcingService _outsourcingService;

        private readonly IRepository<Outsourcing> _outsourcingRepository;

        public OutsourcingController(IOutsourcingService outsourcingService, IRepository<Outsourcing> outsourcingRepository)
        {
            _outsourcingService = outsourcingService;
            _outsourcingRepository = outsourcingRepository;
        }

        public IActionResult OutsourcingIndex()
        {
            return View();
        }

        /// <summary>
        /// 显示添加外协入库单的页面
        /// </summary>
        /// <returns></returns>
        public IActionResult OutsourcingAdd()
        {
            return View();
        }

        /// <summary>
        /// 显示编辑外协入库单的页面
        /// </summary>
        /// <param name="id">外协入库单ID</param>
        /// <returns></returns>
        public IActionResult OutsourcingEdit(int id)
        {
            return View();
        }

        /// <summary>
        /// 获取外协入库列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> OutsourcingList([FromQuery] OutsourcingPageListDTO param)
        {
            // 1. 构建查询
            var query = await _outsourcingRepository
                .GetQueryableAsync();

            // 2. 应用筛选条件
            if (!string.IsNullOrEmpty(param.InboundCode))
            {
                query = query.Where(x => x.InboundCode.Contains(param.InboundCode));
            }

            if (!string.IsNullOrEmpty(param.WorkOrders))
            {
                query = query.Where(x => x.WorkOrders.Contains(param.WorkOrders));
            }

            if (!string.IsNullOrEmpty(param.Vendor))
            {
                query = query.Where(x => x.Vendor.Contains(param.Vendor));
            }

            // 3. 计算总记录数
            var totalCount = await query.CountAsync();

            // 4. 执行分页查询
            var items = await query
                .OrderBy(x => x.InboundCode)
                .Skip((param.PageIndex - 1) * param.PageSize)
                .Take(param.PageSize)
                .ToListAsync();

            // 5. 构建返回结果
            return Json(new
            {
                code = 0,
                msg = "success",
                count = totalCount,
                data = items
            });
        }

        /// <summary>
        /// 获取物料列表数据
        /// </summary>
        [HttpGet]
        public async Task<List<Material>> GetListAsync()
        {
            var result = await _outsourcingService.GetListAsync();
            return result;
        }

        /// <summary>
        /// 根据ID获取外协入库单
        /// </summary>
        /// <param name="id">外协入库单ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetByIdOutsourcing(int id)
        {
            try
            {
                var outsourcing = await _outsourcingRepository.GetAsync(e=>e.Id == id);
                return Json(new { success = true, data = outsourcing });
            }
            catch
            {
                return Json(new { success = false, message = "获取数据失败" });
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateOutsourcing([FromBody] OutsourcingAddDTO param)
        {
            var result = await _outsourcingService.UpdateOutsourcing(param);
            return Json(new { success = result > 0, message = result > 0 ? "修改成功" : "修改失败" });
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeleteOutsourcing(int id)
        {
            await _outsourcingService.DeleteOutsourcing(id);
            return Json(new { success = true, message = "删除成功" });
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddProcessConfiguration([FromBody] OutsourcingAddDTO param)
        {
            var result = await _outsourcingService.AddOutsourcing(param);
            return Json(new { success = result > 0, message = result > 0 ? "添加成功" : "添加失败" });
        }
        /// <summary>
        /// 执行入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateIsLock(int id)
        {
            await _outsourcingService.UpdateIsLock(id);
            return Json(new { success = true, message = "入库成功" });
        }
    }
}
